Select_Cursor_X .equ appbackupscreen +27
Select_Cursor_Y .equ appbackupscreen + 28


;If  Select_Mode is RST (=0), then the map will scroll normally.
;When Select_Mode is on, the player uses a cursor to select things.

asm_flag1_0 .equ 0
asm_flag1_1 .equ 1
#define Select_Mode asm_flag1_0
#define Wait_Between_Presses asm_flag1_1


#macro DrawHL(y, x)

 
 push de
 push bc
 push af
 push ix
 push hl
 LD hl,x*256+y
 LD (curRow),hl
 pop hl
 push hl
 B_CALL _DispHL
 B_CALL _getKey
 pop hl
 pop ix
 pop af
 pop bc
 pop de
 
#endmacro
	
Tile_width = 2
Map_Entry_Size = 2
MAP_MASK = $ff
PMAPWIDTH = 6
PMAPHEIGHT = 4
PMAPHIEGHT = PMAPHEIGHT
delta = savesscreen


Start:

;For displaying buildings-------------
	ld a, 0
	ld (BuildingCount), a



	ld hl,21000
	ld de,$9d95
	bcall(_InsertMem)

Objects_Address .equ 10000

MapData .equ $9D95 + 12500
TileData .equ $9D95 + 19000
	

;To optimize the drawing of structures

Store_Structure_Sprite_Addresses:

	B_CALL _Load_Sprite_Data
	B_CALL _Load_Map_Data
	B_CALL _Load_Tile_Data


	


	
	
	ld hl,2
	ld (delta),hl
	ld a,1
	ld (Num_Layers),a
	xor a
	ld h,a
	ld a,(mapdata)
	ld l,a
	ld (mapheight),hl
	ld a,(mapdata+1)
	ld l,a
	ld (Mapwidth),hl
	ld hl,0
	ld (MapY),hl
	ld (MapX),hl
	ld hl,TileData
	ld (Tileptr),hl
	ld hl,MapData+2
	ld (MAPptr),hl
	ld hl,Mapbuf1
	ld (MapbufPtr),hl
	ld hl,premap
	ld (PreMapPTR),hl
	call DrawMapFull

Loop:
	
	call updatemap
	;call DrawMapFull
	
	ld a,(MapY)
	and tile_width*8-1
	ld h,a
	ld e,PMAPWIDTH*Tile_width
	call HxE

	ld de,mapbuf1
	add hl,de


	;call safecopy


	;ld a,(MapY)
	;and tile_width*8-1
	;ld h,a
	;ld e,PMAPWIDTH*Tile_width
	;call HxE

	;ld de,mapbuf1
	;add hl,de




	
	ld de, plotsscreen
	ld bc, 768
	ldir

Draw_Structures_On_The_Map:
	
	call Draw_Structures_On_Map 
	BIT Select_Mode, (IY + asm_flag1) 
	call nz, Draw_Select_Cursor   ; If Draw_Select_Cursor
					; is equal to 1, 
					; draw the cursor rather
					; then simply scroll the map empty
Keyloop:
	ld b,0
Keyloop2:
	inc b
	ld a,$FF
	out (1),a
	ld a,$80
	out (1),a
	nop
	nop
	in a,(1)
	inc a
	jr nz,_
	ld a,b
	cp 2
	jr nz, Keyloop2
	RES Wait_Between_Presses, (IY + asm_flag1)

	jr Keyloop
_
	

	ld a,$FF
	out (1),a
	ld a,$FE
	out (1),a
	nop
	nop
	in a,(1)
	bit 0,a
	call z,down
	bit 1,a
	call z,left
	bit 2,a
	call z,right
	bit 3,a
	call z,up

; For 2nd Key, testing the displaying and placement of structures
	ld a,$FF
	out (1),a
	ld a, $FD
	out (1),a
	nop
	nop
	in a, (1)
	bit 1,a
	call z, AddObject


	ld a,$FF
	out (1),a
	ld a, $BF
	out (1),a
	nop
	nop
	in a, (1)

	ld a,$FF
	out (1),a
	ld a, $BF
	out (1),a
	nop
	nop
	in a, (1)
	cp $DF
	jr nz, _
	
	BIT Object_Ready_To_Add, (IY + asm_flag1)
	jr z, _
	CALL AddObjectToGame
	

_

	
	CP $7F
	jr nz, _
	ld h,0
	ld l,a
	

	
	BIT Wait_Between_Presses, (IY + asm_flag1)
	
	call z, Rotate_Select_Mode
	
;=================================================================

_
	ld a,$FF
	out (1),a
	ld a,$BF
	out (1),a
	nop
	nop
	in a,(1)
	ld hl,(delta)
	bit 0,a
	jr nz,_
	ld hl,8
_
	bit 1,a
	jr nz,_
	ld hl,6
_
	bit 2,a
	jr nz,_
	ld hl,4
_
	bit 3,a
	jr nz,_
	ld hl,2
_
	bit 4,a
	jr nz,_
	ld hl,1
_
	ld (delta),hl

	bit 5,a
	jr nz,skipspeedon
	in a,(2)
	rla
	jr nc,skipspeedon
	ld a,1
	out ($20),a
	ld a,$FF
skipspeedon:
	bit 6,a
	jr nz,skipspeedoff
	in a,(2)
	rla
	jr nc,skipspeedoff
	xor a
	out ($20),a
	ld a,$FF
skipspeedoff:

	ld a,$FF
	out (1),a
	ld a,$FD
	out (1),a
	nop
	nop
	in a,(1)
	bit 6,a
	jp nz,loop
	ld hl,$9D95
	ld de, 12000
	bcall(_DelMem)
	bcall(_JForceCmdNoChar)

down:


	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Down
movemapdown:
	ld hl,(mapY)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapheight)
		ld de,PMAPHEIGHT-2
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	call cphlde
	jr c,okaydown
	ex de,hl
okaydown:
	ld (mapY),hl
	ret
up:

	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Up
movemapup:
	ld hl,(mapY)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroup
	ld hl,0
notzeroup:
	ld (mapY),hl
	ret
right:

	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Right

movemapright:

	ld hl,(mapx)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapwidth)
		ld de,PMAPWIDTH
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	ex de,hl
	call cphlde
	jr c,okayright
	ex de,hl
okayright:
	ld (mapx),hl
	ret
left:

	BIT Wait_Between_Presses, (IY + asm_flag1)
	RET nz
	BIT Select_Mode, (IY + asm_flag1)
	JP nz, Move_Select_Cursor_Left

movemapleft:

	ld hl,(mapx)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroleft
	ld hl,0
notzeroleft:
	ld (mapx),hl
	ret
CpHLDE:
	or a
	sbc hl,de
	add hl,de
	ret
	
SafeCopy:	
	di
	ld c,$10
	ld a,$80
setrow:
	in f,(c)
	jp m,setrow
	out ($10),a
	ld de,12
	ld a,$20
col:
	in f,(c)
	jp m,col
	out ($10),a
	push af
	ld b,64
row:
	ld a,(hl)
rowwait:
	in f,(c)
	jp m,rowwait
	out ($11),a
	add hl,de
	djnz row
	pop af
	dec h
	dec h
	dec h
	inc hl
	inc a
	cp $2c
	jp nz,col
	ei
	ret

PMAPBUFSIZE = PMAPWIDTH*PMAPHEIGHT*8*Tile_width*Tile_width
PREMAPBUFSIZE = (PMAPWIDTH+1)*PMAPHEIGHT
PreMap = $9d95
Mapbuf1 = $9D95+2500
MapperRam = savesscreen+2

#include "Graymapper.inc"

